using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.EnterpriseSingleSignOn.Interop;

namespace BizTalk.BuildGenerator.Tasks.SSO
{
    /// <summary>
    /// Creates a set of SSO applications based on the details of a file
    /// </summary>
    public class CreateConfigStoreApplication : BaseSSOTask
    {
        private string _ApplicationName;
        private string _AdministratorGroup;
        private string _UserGroup;
        private string _Description;
        private string _CommaDelimitedFieldNames;

        [Required]
        public string CommaDelimitedFieldNames
        {
            get { return _CommaDelimitedFieldNames; }
            set { _CommaDelimitedFieldNames = value; }
        }

        [Required]
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        [Required]
        public string UserGroup
        {
            get { return _UserGroup; }
            set { _UserGroup = value; }
        }
        [Required]
        public string AdministratorGroup
        {
            get { return _AdministratorGroup; }
            set { _AdministratorGroup = value; }
        }
        [Required]
        public string ApplicationName
        {
            get { return _ApplicationName; }
            set { _ApplicationName = value; }
        }

        
        /// <summary>
        /// execute task
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            string[] fieldNames = this._CommaDelimitedFieldNames.Split(",".ToCharArray());
            int noFields = fieldNames.Length + 1;

            int appFlags = 0;

            //bitwise operation for flags
            appFlags |= SSOFlag.SSO_FLAG_APP_CONFIG_STORE;
            appFlags |= SSOFlag.SSO_FLAG_SSO_WINDOWS_TO_EXTERNAL;
            appFlags |= SSOFlag.SSO_FLAG_APP_ALLOW_LOCAL;

            ISSOAdmin admin = new ISSOAdmin();            
            admin.CreateApplication(this._ApplicationName, this._Description, "", this._UserGroup, this._AdministratorGroup, appFlags, noFields);
                        
            int fieldFlags = 0;            
            admin.CreateFieldInfo(this._ApplicationName, "Reserved", fieldFlags);
            foreach (string fieldName in fieldNames)
            {
                admin.CreateFieldInfo(this._ApplicationName, fieldName, fieldFlags);
            }

            admin.UpdateApplication(this._ApplicationName, null, null, null, null, SSOFlag.SSO_FLAG_ENABLED, SSOFlag.SSO_FLAG_ENABLED);

            ConfigurationPropertyBag propertiesBag = new ConfigurationPropertyBag();
            foreach (string field in fieldNames)
            {
                object val = "<empty>";
                propertiesBag.Write(field, ref val);                
            }
            SSOConfiguration.Write(this._ApplicationName, propertiesBag);

            return true;
        }
    }
}
